#include "graphics.h"
#include "luaWrapper.h"

GraphicsMap Graphics::getGraphics(const std::string &filename)
{
  return getGraphics(QStringList(filename.c_str()));
}

GraphicsMap Graphics::getGraphics(const QStringList &files)
{
  GraphicsMap ret;

  LuaWrapper lua;
  foreach(QString f, files)
  {
    lua.load(f);
  }

  lua_getglobal(lua.mLua, "graphics");
  if (lua_istable(lua.mLua, -1))
  {
    for(lua.initTableIterator(); lua.nextTableIteration();)
    {
      if (lua_isstring(lua.mLua, -2) && lua_isstring(lua.mLua, -1))
      {
        Graphics g;
        g.name = lua_tostring(lua.mLua, -2);
        g.filename = lua_tostring(lua.mLua, -1);
        g.pixmap = QPixmap(g.filename);
        ret[g.name] = g;
      }
    }
  }
  else
  {
    lua.warning("could not find graphics table");
  }

  return ret;
}
